home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / wvnsrc75.zip / WVSOCK.C < prev    next >
C/C++ Source or Header  |  1993-02-04  |  4KB  |  192 lines

  1. /*--  First line of WVSock.c ------------------------------------ */
  2. #include <winsock.h>
  3. #include "wvglob.h"
  4. #include "winvn.h"
  5. //#include <errno.h>
  6. #include <stdlib.h>
  7. #include <stdio.h> // for sprintf
  8.  
  9. #include <memory.h>
  10. #include <dos.h>
  11.  
  12. #define MAXSENDLEN 32767
  13. #define COMMBUFSIZE  1600
  14.  
  15. char CommBuff[COMMBUFSIZE];
  16. int CommBuffIdx = COMMBUFSIZE + 1;
  17. int CharsInCommBuff = 0;
  18.  
  19. struct sockaddr addr;
  20. struct sockaddr from;
  21. struct sockaddr_in sa_in;
  22.  
  23. SOCKET NNTPSock;
  24.  
  25. WSADATA winsock_data;
  26.  
  27. int DebugSock = 0;
  28. char mymesbuf[80];
  29.  
  30. unsigned long taddr_n, taddr_h;
  31. short tport_n, tport_h;
  32.  
  33. /*-- function SetupSock --------------------------------------------
  34.  *
  35.  *   Setups up BSD-style socket to the NNTP server.
  36.  *
  37.  *   Returns 1 upon success, else 0.
  38.  */
  39. int SetupSock ()
  40. {
  41.     struct hostent far *hostentry;
  42.     unsigned long ip_address;
  43.  
  44.  
  45.     if ((ip_address=inet_addr (NNTPHost)) != INADDR_NONE) {
  46.         sa_in.sin_addr.s_addr = ip_address;
  47.     }
  48.     else {
  49.         if ((hostentry = gethostbyname( NNTPHost )) == NULL) {
  50.             MessageBox (hWndConf, "Can't resolve hostname.", "WinVn", MB_OK);
  51.             exit (1);
  52.         }
  53.         sa_in.sin_addr.s_addr = *(long far *)hostentry->h_addr;
  54.         }
  55.  
  56.     if ((NNTPSock = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET) {
  57.         MessageBox (hWndConf, "Have you loaded the network?", "socket() failed", MB_OK);
  58.         exit (1);
  59.     }
  60.  
  61.     sa_in.sin_family = AF_INET;
  62.     sa_in.sin_port = htons (NNTPPort);
  63.  
  64.     if (connect( NNTPSock, (struct sockaddr *)&sa_in, sizeof(struct sockaddr_in))==SOCKET_ERROR) {
  65.         MessageBox( hWndConf, "Could not connect to News Server", "WinVn", MB_OK);
  66.         closesocket( NNTPSock );
  67.         exit (0);
  68.     }
  69.  
  70.     return (1);
  71. }
  72.  
  73. /* MRRReadComm PutCommLine MRRInitComm MRRCloseComm */
  74.  
  75. /*-- function MRRReadComm ---------------------------------------
  76.  *
  77.  *  Reads characters from either a serial line or TCP socket.
  78.  */
  79. int MRRReadComm()
  80. {
  81.     int bytesread;
  82.     int myerr;
  83.     u_long lIoctlarg;
  84.  
  85.     myerr = 0;
  86.  
  87.     if (CommBuffIdx >= CharsInCommBuff) {
  88.         ioctlsocket( NNTPSock, FIONREAD, &lIoctlarg );
  89.  
  90.         if (lIoctlarg == 0)
  91.             bytesread = 0;
  92.         else
  93.             bytesread = recv( NNTPSock, CommBuff, COMMBUFSIZE, 0 );
  94.  
  95.         if (bytesread == SOCKET_ERROR)
  96.             myerr = WSAGetLastError();
  97.  
  98.         if (!bytesread || (myerr == WSAEWOULDBLOCK) || (myerr == WSAEINPROGRESS)) {
  99.             return (-1);
  100.         }
  101.         else if (bytesread > 0) {
  102.             CharsInCommBuff = bytesread;
  103.             CommBuffIdx = 0;
  104.         }
  105.  
  106.         else {
  107.             sprintf (mymesbuf, "NNTPSock=%d  bytesread=%d  neterrno=%d", NNTPSock, bytesread, WSAGetLastError());
  108.             MessageBox (hWndConf, mymesbuf, "Unexpected error in MRRReadComm", MB_OK);
  109.             return (-1);
  110.         }
  111.     }
  112.     return (0xff & CommBuff[CommBuffIdx++]);
  113. }
  114.  
  115.  
  116. /*-- function PutCommLine ----------------------------------------------
  117.  *
  118.  *  Entry   line   points to a buffer of characters.
  119.  *          nchars is the number of characters to output.
  120.  */
  121. void
  122. putline_internal (char * line, unsigned int nchars)
  123. {
  124.   int num_sent;
  125.  
  126.   while (nchars > 0) {
  127.       num_sent = send (NNTPSock, line, nchars, 0);
  128.     if (num_sent == SOCKET_ERROR)
  129.         MessageBox (hWndConf, "Error in send ()", "WinVN", MB_OK);
  130.     else {
  131.       nchars -= num_sent;
  132.       line += num_sent;
  133.     }
  134.   }
  135. }
  136.  
  137. void
  138. PutCommLine (line, nchars)
  139.      char *line;
  140.      unsigned int nchars;
  141. {
  142.   putline_internal (line, nchars);
  143.   putline_internal ("\n", 1);
  144. }
  145.  
  146.  
  147. /*-- function MRRCloseComm -----------------------------------------
  148.  *
  149.  *  Close the communications port, serial or TCP.
  150.  */
  151. void MRRCloseComm ()
  152. {
  153.   closesocket( NNTPSock );
  154.   WSACleanup();
  155. }
  156.  
  157. MRRInitComm()
  158. {
  159.   int retcode;
  160.  
  161.   retcode = WSAStartup (1, &winsock_data);
  162.  
  163.   switch (retcode) {
  164.    case 0:
  165.     // successful
  166.     break;
  167.    case WSASYSNOTREADY:
  168.     MessageBox (hWndConf, "Network Not Initialized", "WinVN", MB_ICONHAND || MB_OK);
  169.     return (-1);
  170.     break;
  171.    case WSAVERNOTSUPPORTED:
  172.    case WSAEINVAL:
  173.     MessageBox (hWndConf, "WINSOCK Version not supported.", "WinVN", MB_ICONHAND || MB_OK);
  174.     return (-1);
  175.     break;
  176.   }
  177.  
  178.   retcode = SetupSock();
  179.  
  180.   if (!retcode) {
  181.     MessageBox (hWndConf, "Can't set up socket", "Initialization error", MB_OK | MB_ICONHAND);
  182.   }
  183.  
  184.   IgnoreCommCh = '\r';
  185.   EOLCommCh = '\n';
  186.  
  187.   CommLinePtr = CommLineIn;
  188.   CommState = ST_ESTABLISH_COMM;
  189.   CommBusy = FALSE;
  190.   return (0);
  191. }
  192.